using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Spinning.Spinning.Entity;

namespace Spinning.Spinning.Unitity
{
    class ActorContactManager : IContactListener
    {
        PhysicConnectManager physicConnectorManager;

        public ActorContactManager(PhysicConnectManager physicConnectorManager) {
            this.physicConnectorManager = physicConnectorManager;
        }

        public void BeginContact(Contact contact)
        {
            PhysicSprite a=null, b=null;

            Sprite _a, _b;
            _a = physicConnectorManager.FindSpriteByBody(contact.GetFixtureA().GetBody());
            _b = physicConnectorManager.FindSpriteByBody(contact.GetFixtureB().GetBody());


            if (_a != null && _b != null && _a.GetType().IsSubclassOf(typeof(PhysicSprite)) && _b.GetType().IsSubclassOf(typeof(PhysicSprite))) {
                a =(PhysicSprite) _a;
                b = (PhysicSprite)_b;
            }


            if (a != null && b != null) {
                a.BeginContact(b);
                b.BeginContact(a);
            }

        }

        public void EndContact(Contact contact)
        {
            PhysicSprite a = null, b = null;

            Sprite _a, _b;
            _a = physicConnectorManager.FindSpriteByBody(contact.GetFixtureA().GetBody());
            _b = physicConnectorManager.FindSpriteByBody(contact.GetFixtureB().GetBody());


            if (_a != null && _b != null && _a.GetType().IsSubclassOf(typeof(PhysicSprite)) && _b.GetType().IsSubclassOf(typeof(PhysicSprite)))
            {
                a = (PhysicSprite)_a;
                b = (PhysicSprite)_b;
            }

            if (a != null && b != null)
            {
                a.EndContact(b);
                b.EndContact(a);
            }
        }

        public void PreSolve(Contact contact, ref Manifold oldManifold)
        {
            
        }

        public void PostSolve(Contact contact, ref ContactImpulse impulse)
        {
            
        }
    }
}
